package com.yiqi.liebang.feature.home.view;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.baiiu.filter.ClassifyBo;
import com.flyco.tablayout.SlidingTabLayout;
import com.luck.picture.lib.rxbus2.RxBus;
import com.suozhang.framework.framework.BaseActivity;
import com.yiqi.liebang.R;
import com.yiqi.liebang.entity.bo.SearchFriendBo;
import com.yiqi.liebang.feature.home.contract.ClassifyContract;
import com.yiqi.liebang.feature.home.dependencies.classsify.ClassifyPresenterModule;
import com.yiqi.liebang.feature.home.dependencies.classsify.DaggerClassifyComponent;
import com.yiqi.liebang.feature.order.model.MessageEvent;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

public class ChoiceExpertActivity extends BaseActivity implements Toolbar.OnMenuItemClickListener, ClassifyContract.View {

    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    private  List<String> mTitles ;
    @BindView(R.id.tl_4)
    SlidingTabLayout mTl4;
    @BindView(R.id.vp)
    ViewPager mVp;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private ArrayList<ClassifyBo> mData = null;

    @Inject
    ClassifyContract.Presenter mPresenter;
    private SearchFriendBo.UserFriendBean mUserUid;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_choice_expert;
    }

    @Override
    protected void initInjector() {
        DaggerClassifyComponent.builder().classifyPresenterModule(new ClassifyPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        mToolbar.inflateMenu(R.menu.menu_search);
        initToolBar(mToolbar, "选择行家", true, true);
        RxBus.getDefault().register(this);
    }

    @Override
    protected void initData() {
        mPresenter.getQuestionClassify();
    }

    @Override
    protected void initEvent() {
        mToolbar.setOnMenuItemClickListener(this);
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        if (item.getItemId() == R.id.action_search) {
            startActivityForResult(new Intent(this, SearchExpertActivity.class),1);
        }
        return false;
    }


    @Override
    public void showUserSuccess(String classifyBos) {

    }

    @Override
    public void showUserError(String classifyBos) {

    }

    @Override
    public void showSuccess(List<ClassifyBo> classifyBos) {
        mData = new ArrayList<>();
        mTitles=new ArrayList<>();
        for (int i = 0; i < classifyBos.size(); i++) {
            mTitles.add(classifyBos.get(i).getClassify());
            mFragments.add(ExpertFragment.getInstance(classifyBos.get(i)));
        }
        String[] strings = new String[mTitles.size()];
        mTitles.toArray(strings);
        mTl4.setViewPager(mVp, strings, this, mFragments);
    }

    @Override
    public void showExpert(List<SearchFriendBo.UserFriendBean> classifyBos) {

    }

    @Override
    public void showEmpty() {

    }

    @Override
    public void showError(String ex) {
        showMsg(ex);
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 2) {
            if (data!=null){
                mUserUid = (SearchFriendBo.UserFriendBean) data.getSerializableExtra("three");
                if (mUserUid!=null){
                    RxBus.getDefault().post(new MessageEvent(mUserUid));
                }
            }
        }}

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxBus.getDefault().unregister(this);
    }
}
